how can I make a single field dependent?

how can I make a single field dependent?

mfmf Posts: 41Questions: 11Answers: 0

Hello!

I have a selection in the editor and you can select your own name and save something in a table. Is it possible to make the select field know what user saved something? so you cannot select your own name a second time for a new row?

I have some code, not sure if that is the best way but I end up in a loop and want to know if there is something to prevent it.

this is the server side PHP script name user_dynamic.php

<?php
require_once 'dbconfig.php';

    //$week = $_REQUEST['values']['lunch.week'];
    $week = 13;
    $stmt = $db_con->prepare("SELECT users FROM lunch WHERE (week = :week)");
    $stmt->execute(array(":week"=>$week));

    $stmt1 = $db_con->prepare("SELECT id FROM users");
    $stmt1->execute();

    $data = array();
    $count = 1;
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        $data[$count++] = $row['users'];
    }  

    $data1 = array();
    $count = 1;
    while($row1 = $stmt1->fetch(PDO::FETCH_ASSOC)){
        $data1[$count++] = $row1['id'];
    }  

   $notFound = array_diff($data1, $data);

   if(!empty($notFound)):  
        foreach($notFound as $id => $user):
            $id = $notFound[$id];
            $ids = $ids . "," . $id;
        endforeach;
    endif; 
    $ids = substr($ids,1);
    $stmt = $db_con->prepare("SELECT id as value, concat(first_name,' ',last_name) as label FROM users WHERE id IN(". $ids . ")");
    $stmt->execute();
    $contacts=$stmt->fetchAll();

    echo json_encode( [
        'options' => [
            'lunch.users' => $contacts
        ]
    ] );

<?php
>  
```
?>


And this is partly the javascript (it was too long)

$(document).ready(function() {

var lunchEditor = new $.fn.dataTable.Editor( {
    //ajax: "php/table.lunch.php",
    ajax: {
        url: 'php/table.lunch.php',
        data: function ( d ) {
            var selected = weekTable.row( { selected: true } );

            if ( selected.any() ) {
                d.week = selected.data().id;
            }
        }
    },
    table: "#lunch",
    fields: [ {
            label: "User:",
            name:  "lunch.users",
            type: "select"
          },{
            label:     "Monday:",
            name:      "lunch.monday",
            type:      "checkbox",
            separator: "|",
            options:   [
                { label: '', value: 1 }
            ],
            unselectedValue: 0
         },{
            label: "Tuesday:",
            name:  "lunch.tuesday",
            type:      "checkbox",
            separator: "|",
            options:   [
                { label: '', value: 1 }
            ],
            unselectedValue: 0
        },{
            label: "Wednesday:",
            name:  "lunch.wednesday",
            type:      "checkbox",
            separator: "|",
            options:   [
                { label: '', value: 1 }
            ],
            unselectedValue: 0
        },{
            label: "Thursday:",
            name:  "lunch.thursday",
            type:      "checkbox",
            separator: "|",
            options:   [
                { label: '', value: 1 }
            ],
            unselectedValue: 0
        },{
            label: "Friday:",
            name:  "lunch.friday",
            type:      "checkbox",
            separator: "|",
            options:   [
                { label: '', value: 1 }
            ],
            unselectedValue: 0
        },{
            label: "Signature:",
            name:  "lunch.sig",
            type:  "sig"
        }
    ]
} );


lunchEditor.dependent( 'lunch.users', 'users_dynamic.php' ); 

} );
```

Its not a public site so I can't show it in action

Thanks!!

Answers

  • mfmf Posts: 41Questions: 11Answers: 0

    never mind I solved it by adding a second select field in editor (lunch.week) and make it dependent on that

This discussion has been closed.